home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP4.TXT < prev    next >
Text File  |  1988-11-27  |  12KB  |  322 lines

  1.            CHAPTER 4 - The Pascal loops and control structures
  2.  
  3.  
  4.             Every program we have examined to this point has been  a
  5.         simple  one pass through with no statements being  repeated.
  6.         As in all other languages, Pascal has extensive capabilities
  7.         to  do looping and conditional branching.   We will look  at
  8.         these now.
  9.  
  10.                                THE FOR LOOP
  11.  
  12.             We  will start with what may be the easiest structure to
  13.         understand,  the FOR loop.   This is used to repeat a single
  14.         Pascal  statement  any  number of  times  we  desire.   Load
  15.         LOOPDEMO and we will discuss the loops presented there. 
  16.  
  17.             The first example is the simplest and is simply a repeat
  18.         of  a WRITELN 7 times.   We have three new  reserved  words,
  19.         FOR,  TO,  and  DO  which  are used as  shown.   Any  simple
  20.         variable of type INTEGER,  BYTE, or CHAR can be used for the
  21.         loop  index  and  it must be defined  in  a  VAR  statement.
  22.         Following   the  DO  reserved  word  is  any  single  Pascal
  23.         statement  that  will be repeated the  specified  number  of
  24.         times.   Note  that  the  loop is an incrementing  loop  but
  25.         substitution  of DOWNTO for TO will make it  a  decrementing
  26.         loop as is illustrated in the last example in this program.
  27.  
  28.                         A COMPOUND PASCAL STATEMENT
  29.  
  30.             The  second  example contains our first compound  Pascal
  31.         statement.  It was mentioned in Chapter 1 that the BEGIN END
  32.         pair of reserved words could be used to mark the limits of a
  33.         compound  statement.   In this case,  the  single  statement
  34.         starting  with the BEGIN and extending through and including
  35.         the  END statement is the single Pascal statement that  will
  36.         be  executed 10 times.   A second variable "total" has  been
  37.         introduced to simply add another operation to the loop.  Any
  38.         valid Pascal operation can be performed within the BEGIN END
  39.         pair, including another loop, thus resulting in nested loops
  40.         to whatever depth you desire. 
  41.  
  42.             The  third example shows how the CHAR variable could  be
  43.         used in a FOR loop.  Pascal requires that the loop variable,
  44.         the starting point,  and the ending point all be of the same
  45.         type or it will generate an error message.
  46.  
  47.             The  fourth example is a decrementing loop as  mentioned
  48.         earlier.
  49.  
  50.                              THE IF STATEMENT
  51.  
  52.             Now   we   will  look  at  the   conditional   branching
  53.         capability, or at least one of them, the IF statement.  Load
  54.         IFDEMO for an onscreen look at the IF THEN pair of  reserved
  55.  
  56.  
  57.                                 Page 15
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.            CHAPTER 4 - The Pascal loops and control structures
  68.  
  69.  
  70.         words.   Any  condition  that  can be reduced to  a  boolean
  71.         answer  is put between the IF THEN pair of  words.   If  the
  72.         resulting  expression resolves to TRUE,  then the  following
  73.         single  Pascal statement is executed,  and if it resolves to
  74.         FALSE,  then the following single statement is skipped over.
  75.         Of course,  you can probably guess that the single statement
  76.         can  be replaced with a compound statement bracketed with  a
  77.         BEGIN END pair and you are correct.  Study example 1 and you
  78.         will  see  that  the line will always  be  printed  in  this
  79.         particular fragment.  It is very difficult to come up with a
  80.         good   example   without  combining  some  of  the   control
  81.         structures which we will do in the next file.
  82.  
  83.             The second example is similar to the first but with  the
  84.         single  statement  replaced  with a compound  statement  and
  85.         should be easy to understand.
  86.  
  87.             The  third example contains a new reserved  word,  ELSE.
  88.         When  the  IF condition is FALSE,  the single  statement  is
  89.         skipped and if a semicolon is encountered,  the IF clause is
  90.         totally complete.   If instead of a semicolon,  the reserved
  91.         word ELSE is encountered,  then the single Pascal  statement
  92.         following ELSE is executed.   One and only one of the single
  93.         statements  will be executed every time the IF statement  is
  94.         encountered  in  the  program.   Examination  of  the  third
  95.         example should clear this up in your mind.
  96.  
  97.                          THE IF-THEN-ELSE "block"
  98.  
  99.             Put  on your thinking cap because the next principle  is
  100.         difficult  to grasp at first but will suddenly clear up  and
  101.         be  one  of  the most useful facts  of  Pascal  programming.
  102.         Since  the  entire IF THEN ELSE "block" of code is itself  a
  103.         single  Pascal  statement,  by definition,  it can  be  used
  104.         anywhere that an executable statement is legal without BEGIN
  105.         END separators.   This is shown in the fourth example of the
  106.         IFDEMO Pascal example program.   The IF THEN ELSE  construct
  107.         is  one of the most used,  most useful,  and therefore  most
  108.         important  aspects  of PASCAL.   For this reason you  should
  109.         become very familiar with it.
  110.  
  111.             Try  changing  some  of the conditions  in  the  example
  112.         program to see if you can get it to print when you expect it
  113.         to just for your own practice and enjoyment.   When you  are
  114.         ready, we will go on to a program with loops and conditional
  115.         statements.
  116.  
  117.                           LOOPS AND IFS TOGETHER
  118.  
  119.             Load  LOOPIF  and  observe it for  a  few  minutes.   It
  120.         contains  most of what you have studied so far and should be
  121.  
  122.  
  123.                                 Page 16
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.            CHAPTER 4 - The Pascal loops and control structures
  134.  
  135.  
  136.         understandable  to you at this point.   It contains  a  loop
  137.         with  two IF statements within it,  and another loop  within
  138.         one of the IF statements.
  139.  
  140.             An easily made error should be pointed out at this time.
  141.         If an extraneous semicolon were put at the end of the second
  142.         IF statement,  the code following the statement would always
  143.         be  executed  because  the  "null"  statement  (the  nothing
  144.         statement  between the THEN and the semicolon) would be  the
  145.         conditional  statement.   The compiler would not generate an
  146.         error and you would get no warning.   Add a semicolon  after
  147.         the IF statement to see the error.
  148.  
  149.                        FINALLY, A MEANINGFUL PROGRAM
  150.  
  151.             Load  TEMPCONV and study its structure.   Run it and you
  152.         will  have  a list of Centigrade to  Fahrenheit  temperature
  153.         conversions with a few added notes.   Load, examine, and run
  154.         DUMBCONV  for a good example of poor variable  naming.   The
  155.         structure  of the program is identical to the  last  program
  156.         and  when you run it,  you will see that it is identical  in
  157.         output,  but  it is difficult to understand what it does  by
  158.         studying the listing.   These programs should both be easily
  159.         understood  by  you  by now,  so we will go on to  our  next
  160.         Pascal control structure.
  161.  
  162.                            THE REPEAT UNTIL LOOP
  163.  
  164.             The next two Pascal structures are very similar  because
  165.         they  are both indefinite loops (indefinite because they are
  166.         not executed a fixed number of times).   One of the loops is
  167.         evaluated at the top and the other at the bottom.   It  will
  168.         probably  be easier to start with the REPEAT UNTIL structure
  169.         which is the loop that is evaluated at the bottom.
  170.  
  171.             Retrieve  the file REPEATLP to see a repeat  loop.   Two
  172.         more  reserved  words are defined here,  namely  REPEAT  and
  173.         UNTIL.   This  rather  simple construct simply  repeats  all
  174.         statements between the two reserved words until the  boolean
  175.         expression following the UNTIL is found to be TRUE.  This is
  176.         the  only  expression I know of that operates on a range  of
  177.         statements  rather  than a single statement  and  BEGIN  END
  178.         delimiters are not required.   A word of caution is in order
  179.         here.   Since  the  loop  is executed until  some  condition
  180.         becomes TRUE,  it is possible that the condition will  never
  181.         be TRUE and the loop will never terminate.
  182.  
  183.                               THE WHILE LOOP
  184.  
  185.             The  file  WHILELP  contains an example of  another  new
  186.         construct,  the WHILE loop.  This uses the WHILE DO reserved
  187.  
  188.  
  189.                                 Page 17
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.            CHAPTER 4 - The Pascal loops and control structures
  200.  
  201.  
  202.         words and will execute one Pascal statement (or one compound
  203.         statement  bounded  with BEGIN END) continuously  until  the
  204.         boolean  expression  between the two  words  becomes  FALSE.
  205.         This  loop is also indeterminate and could,  like the REPEAT
  206.         UNTIL,  never terminate.  You should therefore exercise care
  207.         in using it. 
  208.  
  209.                             THE CASE STATEMENT
  210.  
  211.             The  final  control  structure  introduces  yet  another
  212.         reserved word,  CASE.  The CASE construct actually should be
  213.         included  with  the IF statement since it is  a  conditional
  214.         execution statement, but I chose to save it for last because
  215.         it is rather unusual and will probably be used less than the
  216.         others we have discussed in this chapter.
  217.  
  218.             The  CASE  statement  is  used to  select  one  of  many
  219.         possible  simple Pascal statements to execute based  on  the
  220.         value  of  a simple variable.   Load the file  CASEDEMO  and
  221.         observe the program for an example of a case statement.  The
  222.         variable  between  the  CASE and OF reserved  words  is  the
  223.         variable  used to make the selection.   Following that,  the
  224.         various selections are given in the form of a possible value
  225.         or  range,  a  colon,  a  single  Pascal  statement,  and  a
  226.         semicolon.  Following the list of selections, an ELSE can be
  227.         added  to cover the possibility that none of the  selections
  228.         were  executed.   Finally,  an  END;  statement is  used  to
  229.         terminate the case construct.   Note that this is one of the
  230.         few  places  in  Pascal  that  an  END  is  used  without  a
  231.         corresponding BEGIN.
  232.  
  233.             The example file uses "count" for a variable and  prints
  234.         the numbers one through five in text form, and declares that
  235.         numbers  outside  this range are not in the allowable  list.
  236.         The  program should be self explanatory beyond  that  point.
  237.         This  is  admittedly  a very brief explanation of  the  CASE
  238.         statement  but  you will have no trouble using it  when  you
  239.         have need for it.  You can study it in detail at that time.
  240.  
  241.             This brings us to the end of chapter 4 and you now  have
  242.         enough  information to write essentially any program desired
  243.         in  Pascal.   There  would  be a  few  difficulties  if  you
  244.         attempted  to  try  to program without  the  further  topics
  245.         coming up in the next chapters.   The additional topics will
  246.         greatly  add  to the flexibility of Pascal and will  greatly
  247.         ease programming in it.
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.                                 Page 18
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.            CHAPTER 4 - The Pascal loops and control structures
  266.  
  267.  
  268.                            PROGRAMMING EXERCISES
  269.  
  270.         1.  Write a program that lists the numbers from 1 to 12  and
  271.             writes a special message beside the number  representing
  272.             your month of birth.
  273.  
  274.         2.  Write  a program that lists all of the numbers from 1 to
  275.             12 except for the numbers 2 and 9.
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                 Page 19
  322.